/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Symbol Matching.
 *
 * Symbol Matching is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Symbol Matching is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Symbol Matching.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.reaktiopeli.xml;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

import fi.honkalampisaatio.reaktiopeli.xml.GameDifficulty.DIFFICULTY;

@XmlAccessorType(XmlAccessType.NONE)
public class HighScore {
	@XmlElement(name = "gamePath")
	private String gamePath;
	// XmLElementWrapper generates a wrapper element around XML representation
	@XmlElementWrapper(name = "scoresByDifficulty")
	// XmlElement sets the name of the entities
	@XmlElement(name = "scores")
	private HashMap<DIFFICULTY, HighScoreEntryList> scoresByDifficulty;
	@XmlElement(name = "maxRows")
	private int maxRows = 5;
	private int lastRowAdded = -1;

	public int getLastRowAdded() {
		return lastRowAdded;
	}
	
	public void setMaxRows(int maxRows) {
		this.maxRows = maxRows;
	}
	public int getMaxRows() {
		return maxRows;
	}
	
	public void setGamePath(String gamePath) {
		this.gamePath = gamePath;
	}
	public String getGamePath() {
		return gamePath;
	}

	public void setScores(DIFFICULTY difficulty, HighScoreEntryList scores) {
		lastRowAdded = -1;
		if(scores==null || difficulty==null)
		{
			return;
		}
		if(scoresByDifficulty==null)
		{
			scoresByDifficulty = new HashMap<DIFFICULTY, HighScoreEntryList>();
		}
		scoresByDifficulty.put(difficulty, scores);
	}
	public ArrayList<GameTime> getScores(DIFFICULTY difficulty) {
		if(difficulty==null)
		{
			return null;
		}
		if(scoresByDifficulty==null)
		{
			scoresByDifficulty = new HashMap<DIFFICULTY,HighScoreEntryList>();
		}
		HighScoreEntryList scores = scoresByDifficulty.get(difficulty);
		if(scores==null)
		{
			scores = new HighScoreEntryList();
			scoresByDifficulty.put(difficulty, scores);
		}
		return scores.getScoreList();
	}
	
	public int addGameTime(DIFFICULTY difficulty, GameTime score)
	{
		lastRowAdded = -1;
		if(score==null || difficulty==null)
		{
			return lastRowAdded;
		}
		if(scoresByDifficulty==null)
		{
			scoresByDifficulty = new HashMap<DIFFICULTY, HighScoreEntryList>();
		}
		HighScoreEntryList scores = scoresByDifficulty.get(difficulty);
		if(scores==null)
		{
			scores = new HighScoreEntryList();
			scoresByDifficulty.put(difficulty, scores);
		}
		ArrayList<GameTime> scoreList = scores.getScoreList();
		Collections.sort(scoreList);

		for(int i=0; i<scoreList.size() && i<maxRows && lastRowAdded<0; i++)
		{
			GameTime tmpScore = scoreList.get(i);
			if(tmpScore.compareTo(score)>=0)
			{
				scoreList.add(i, score);
				lastRowAdded = i;
			}
		}
		if(lastRowAdded<0 && scoreList.size()<maxRows)
		{
			scoreList.add(score);
			lastRowAdded = scoreList.size()-1;
		}
		while(scoreList.size()>maxRows)
		{
			scoreList.remove(maxRows);
		}
		
		return lastRowAdded;
	}

	public HashMap<DIFFICULTY, HighScoreEntryList> getScoresByDifficulty() {
		return scoresByDifficulty;
	}

	public void setScoresByDifficulty(HashMap<DIFFICULTY, HighScoreEntryList> scoresByDifficulty) {
		this.scoresByDifficulty = scoresByDifficulty;
	}
}
